package cn.edu.lingnan.lsqn.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.cdahua.basic.model.SystemContext;
import cn.edu.lingnan.lsqn.model.Group;
import cn.edu.lingnan.lsqn.model.LsqnException;
import cn.edu.lingnan.lsqn.model.Role;
import cn.edu.lingnan.lsqn.model.RoleType;
import cn.edu.lingnan.lsqn.model.User;
import cn.edu.lingnan.lsqn.service.IUserService;
import cn.edu.lingnan.lsqn.util.Captcha;
import cn.edu.lingnan.lsqn.web.SessionContext;

@Controller
public class IndexController {
	
	@Autowired
	private IUserService userService;
	
	
	@RequestMapping(value={"/","/login"},method=RequestMethod.GET)
	public String login(){
		return "index/login";
	}
	@RequestMapping(value={"/","/login"},method=RequestMethod.POST)
	public String login(User u,@RequestParam String vc,Model model,HttpSession session){
		
		
		String cc = (String) session.getAttribute("cc");
		if(!cc.equals(vc)) throw new LsqnException("验证码错误");
		
		User user = userService.login(u);
		user.setLastLoginDate(new Date());
		userService.update(user);
		session.setAttribute("loginUser", user);
		SystemContext.removeOrder();
		SystemContext.removeSort();
		List<Role> roles = userService.listUserRole(user.getId());
		List<Group> groups = userService.listUserGroup(user.getId());
		boolean isGAdmin = isRole(roles, RoleType.ROLE_GENERALADMIN);
		boolean isSAdmin = isRole(roles, RoleType.ROLE_SUPERADMIN);
		boolean isStudent = isRole(roles, RoleType.ROLE_STUDENTAUTHOR);
		boolean isTeacher = isRole(roles, RoleType.ROLE_TEACHERAUTHOR);
		session.setAttribute("isGAdmin", isGAdmin);
		session.setAttribute("isSAdmin", isSAdmin);
		session.setAttribute("isStudent", isStudent);
		session.setAttribute("isTeacher", isTeacher);
		if(isGroup(groups, "新媒体")){
			for(int i = 1; i <= 4 ; i++){
				session.setAttribute("column"+i, true);
			}
		}else if(isGroup(groups, "广播台")){
			session.setAttribute("column1", false);
			session.setAttribute("column2", true);
			session.setAttribute("column3", false);
			session.setAttribute("column4", false);
		}else if(isGroup(groups, "教师")){			
			session.setAttribute("column1", false);
			session.setAttribute("column2", false);
			session.setAttribute("column3", true);
			session.setAttribute("column4", false);
		}else{
			session.setAttribute("column1", true);
			session.setAttribute("column2", true);
			session.setAttribute("column3", false);
			session.setAttribute("column4", false);
		}
		session.removeAttribute("cc");
		SessionContext.addSessoin(session);
		return "redirect:index";
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		SystemContext.removeRealPath();
		SessionContext.removeSession(session);
		session.invalidate();
		return "redirect:login";
	}
	
	private boolean isGroup(List<Group> gs, String g_name) {
		for(Group g:gs) {
			if(g.getName().equals(g_name))
				return true;
		}
		return false;
	}
	
	private boolean isRole(List<Role> rs,RoleType rt) {
		for(Role r:rs) {
			if(r.getRoleType()==rt) return true;
		}
		return false;
	}
	
	
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(User u,Model model,@RequestParam String vc,HttpSession session){
		
		String cc = (String) session.getAttribute("cc");
		
		if(!cc.equals(vc)) throw new LsqnException("验证码错误");
		

		Integer[] i = new Integer[]{3};
		userService.add(u, null, i);
		User user = userService.loadByEmail(u.getEmail());
		model.addAttribute("loginUser", user);
		session.removeAttribute("cc");
		return "redirect:index";
	}
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String register(){
		return "index/register";
	}
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String index(){
		return "index/index";
	}
	
	@RequestMapping("/drawCheckCode")
	public void drawCheckCode(HttpServletResponse resp,HttpSession session) throws IOException {
		resp.setContentType("image/jpg");
		int width = 120;
		int height = 30;
		Captcha c = Captcha.getInstance();
		c.set(width, height);
		String checkcode = c.generateCheckcode();
		session.setAttribute("cc", checkcode);
		OutputStream os = resp.getOutputStream();
		ImageIO.write(c.generateCheckImg(checkcode), "jpg", os);
	}
}
